package org.example.core.util;

import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class AnnotationStoreUtils {

    private static final Map<String, Map<String, Object>> store = new HashMap<>();

    public static void putValue(String className, String fieldName, Object fieldValue) {
        Map<String, Object> map = store.get(className);
        if (map == null) {
            map = new HashMap<>();
            store.put(className, map);
        }
        map.put(fieldName, fieldValue);
    }

    public static void fill(Object $this) {
        try {
            Class<?> clazz = $this.getClass();
            String canonicalName = clazz.getCanonicalName();
            Map<String, Object> map = store.get(canonicalName);
            if (map != null) {
                for (Map.Entry<String, Object> entry : map.entrySet()) {
                    Field field = clazz.getDeclaredField(entry.getKey());
                    field.setAccessible(true);
                    field.set($this, entry.getValue());
                }
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

}
